package com.example.upload;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class SecondUpContent extends AsyncTask<Integer, Integer, String> {

	private String url;
	private String headerKey;
	private String headerValue;
	private String headerKey1;
	private String headerValue1;
	private String headerKey2;
	private String headerValue2;
	private List<Map<String, String>> attchlist;
	private Context mContext;

	public SecondUpContent(Context context, List<Map<String, String>> list) {
		this.mContext = context;
		this.attchlist = list;
	}

	@Override
	protected String doInBackground(Integer... arg0) {
		url = "";// 这个是另一个服务的路径地址
		headerKey = "Content-Type";
		headerValue = "application/json";
		headerKey1 = "assignmentid";
		headerValue1 = "";// 从前台传递过来 可以利用构造函数
		headerKey2 = "username";
		headerValue2 = "";// 从前台传递过来
		String result;
		try {
			HttpPost request = new HttpPost(url);
			Log.i("Upload", "=========二次 异步 list====" + attchlist);
			request.addHeader(headerKey1, headerValue1);
			request.addHeader(headerKey2, headerValue2);
			request.addHeader(headerKey, headerValue);
			JSONObject param1 = new JSONObject();
			param1.put("content", "gdcguasdguyguyg");// 从前台传递过来
			param1.put("attachids", attchlist);
			// 绑定到请求 Entry
			StringEntity se = new StringEntity(param1.toString());
			Log.i("test", "----se-------------" + se);
			request.setEntity(se);
			// 发送请求
			HttpResponse httpResponse = new DefaultHttpClient().execute(request);
			if (httpResponse.getStatusLine().getStatusCode() == 297) {
				try {
					// result =
					// EntityUtils.toString(httpResponse.getEntity(),"UTF_8");
					result = "save success!!";
					return result;
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return "save failed!!";
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(String result) {
		Toast.makeText(mContext, result, 1).show();
		super.onPostExecute(result);
	}

}
